package com.zry.fertilizer.presenter

import android.support.v7.widget.AppCompatSpinner
import android.view.View
import android.widget.Adapter
import android.widget.AdapterView
import com.zry.fertilizer.R
import com.zry.fertilizer.been.FlowCode
import com.zry.fertilizer.been.ModelBean
import com.zry.fertilizer.been.ProductInfo
import com.zry.fertilizer.been.ResultBean
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.activity.BatchCreateActivity
import com.zry.fertilizer.utils.ACacheUtil
import com.zry.fertilizer.utils.ToastUtil
import com.zry.fertilizer.view.ModelSpinnerAdapter
import com.zry.fertilizer.view.ProductNameSpinnerAdapter
import kotlinx.android.synthetic.main.activity_batch_create.*

class BatchCreatePresenter(private val activity: BatchCreateActivity) : ShowUserView {


    private var productInfoList: MutableList<ProductInfo>? = null
    private var productInfo: ProductInfo? = null
    private var modelBean:ModelBean?=null

    fun createBatchClick(view: View) {
        when (view.id) {
            R.id.submitCreateBatchBtn -> {
                if (activity.enterBatchName.text.toString().trim().isEmpty()) {
                    return ToastUtil.toast(activity, "批次名称不能为空")
                }
                if (productInfo!!.ProductID.isEmpty()) {
                    return ToastUtil.toast(activity, "产品名称不能为空")
                }
                if (modelBean!!.ModelID.isEmpty()) {
                    return ToastUtil.toast(activity, "关联模板不能为空")
                }

                HttpServer(this).addBatchInfo(
                        ACacheUtil().getLoginInfo().EntID,
                        ACacheUtil().getLoginInfo().userid,
                        activity.enterBatchName.text.toString().trim(),
                        productInfo!!.ProductID,
                        modelBean!!.ModelID,
                        activity)
            }
        }
    }

    //获取产品列表
    fun getProductList() {
        HttpServer(this).getProductList(ACacheUtil().getLoginInfo().EntID, activity)
    }

    //获取模板列表
    fun getBatchModelList() {
        HttpServer(this).onLoadingBatchModel(ACacheUtil().getLoginInfo().EntID, productInfo!!.ProductID, activity)
    }


    private fun initProductName(selectProductName: AppCompatSpinner) {
        val productNameSpinnerAdapter = ProductNameSpinnerAdapter(activity, productInfoList!!)
        selectProductName.adapter = productNameSpinnerAdapter
        spinnerItemSelect(selectProductName)
    }

    private fun initModelSpinner(selectAssociationTemplate: AppCompatSpinner, modelList: MutableList<ModelBean>) {
        val modelSpinnerAdapter = ModelSpinnerAdapter(activity, modelList)
        selectAssociationTemplate.adapter = modelSpinnerAdapter
        spinnerItemSelect(selectAssociationTemplate)
        selectAssociationTemplate.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(p0: AdapterView<*>?) =Unit
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
                modelBean=modelList[position]
            }
        }
    }


    private fun spinnerItemSelect(appCompatSpinner: AppCompatSpinner) {
        appCompatSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, view: View?, position: Int, p3: Long) {
                when (appCompatSpinner) {
                    activity.selectProductName -> {
                        productInfo = productInfoList!![position]
                        activity.batchCreateBinding?.productInfo = productInfo
                        getBatchModelList()
                    }
//                    activity.selectAssociationTemplate -> {
//                        modelBean = modelList!![position]
//                    }
                }
            }

            override fun onNothingSelected(p0: AdapterView<*>?) = Unit
        }
    }

    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            1 -> {
                productInfoList = data as MutableList<ProductInfo>
                initProductName(activity.selectProductName)
            }
            30 -> {
                modelBean=ModelBean("","")
                val modelList=data as MutableList<ModelBean>
                initModelSpinner(activity.selectAssociationTemplate,modelList)
            }
            31 -> {
                val resultData = data as ResultBean<FlowCode>
                if (resultData.State == "1") {
                    activity.finish()
                    ToastUtil.toast(activity, "批次创建成功")
                } else {
                    ToastUtil.toast(activity, resultData.Message)
                }
            }
        }
    }

}